import service, { axiosInstance } from './service'
import { CONTENT_TYPE } from '@/constants'
import { useUserStoreWithOut } from '@/store/modules/user'

const request = (option: AxiosConfig) => {
  const { url, method, params, data, headers, responseType } = option

  const userStore = useUserStoreWithOut()
  return service.request({
    url: url,
    method,
    params,
    data: data,
    responseType: responseType,
    headers: {
      'Content-Type': CONTENT_TYPE,
      [userStore.getTokenKey ?? 'Authorization']: userStore.getToken ?? '',
      ...headers
    }
  })
}

// 导出基础的请求方法
export const http = {
  get: <T = any>(option: AxiosConfig) => {
    return request({ method: 'get', ...option }) as Promise<IResponse<T>>
  },
  post: <T = any>(option: AxiosConfig) => {
    return request({ method: 'post', ...option }) as Promise<IResponse<T>>
  },
  delete: <T = any>(option: AxiosConfig) => {
    return request({ method: 'delete', ...option }) as Promise<IResponse<T>>
  },
  put: <T = any>(option: AxiosConfig) => {
    return request({ method: 'put', ...option }) as Promise<IResponse<T>>
  },
  cancelRequest: (url: string | string[]) => {
    return service.cancelRequest(url)
  },
  cancelAllRequest: () => {
    return service.cancelAllRequest()
  }
}

// 新增：直接使用 axios 实例的封装，支持 transformRequest 等配置
export const directAxios = {
  post: <T = any>(url: string, data?: any, config?: any) => {
    const userStore = useUserStoreWithOut()


    const defaultConfig = {
      headers: {
        'Content-Type': CONTENT_TYPE,
        [userStore.getTokenKey ?? 'Authorization']: userStore.getToken ?? '',
        ...config?.headers
      },
      ...config
    }
    return axiosInstance.post(url, data, defaultConfig) as Promise<IResponse<T>>
  },
  get: <T = any>(url: string, config?: any) => {
    const userStore = useUserStoreWithOut()
    const defaultConfig = {
      headers: {
        'Content-Type': CONTENT_TYPE,
        [userStore.getTokenKey ?? 'Authorization']: userStore.getToken ?? '',
        ...config?.headers
      },
      ...config
    }
    return axiosInstance.get(url, defaultConfig) as Promise<IResponse<T>>
  },
  put: <T = any>(url: string, data?: any, config?: any) => {
    const userStore = useUserStoreWithOut()
    const defaultConfig = {
      headers: {
        'Content-Type': CONTENT_TYPE,
        [userStore.getTokenKey ?? 'Authorization']: userStore.getToken ?? '',
        ...config?.headers
      },
      ...config
    }
    return axiosInstance.put(url, data, defaultConfig) as Promise<IResponse<T>>
  },
  delete: <T = any>(url: string, config?: any) => {
    const userStore = useUserStoreWithOut()
    const defaultConfig = {
      headers: {
        'Content-Type': CONTENT_TYPE,
        [userStore.getTokenKey ?? 'Authorization']: userStore.getToken ?? '',
        ...config?.headers
      },
      ...config
    }
    return axiosInstance.delete(url, defaultConfig) as Promise<IResponse<T>>
  }
}

// 新增：专门处理表单数据的便捷方法
export const formPost = {
  post: <T = any>(url: string, data: any, additionalConfig?: any) => {
    const userStore = useUserStoreWithOut()
    const config = {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        [userStore.getTokenKey ?? 'Authorization']: userStore.getToken ?? '',
        ...additionalConfig?.headers
      },
      transformRequest: [(data: any) => {
        if (typeof data === 'object' && !(data instanceof FormData)) {
          return new URLSearchParams(data).toString()
        }
        return data.toString()
      }],
      ...additionalConfig
    }
    return axiosInstance.post(url, data, config) as Promise<IResponse<T>>
  }
}

export default http
